package com.hongkuncheng.cms.controller.admin;

import java.io.File;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.DirectoryHelper;
import com.hongkuncheng.cms.helper.FileHelper;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Zhuanti;
import com.hongkuncheng.cms.model.Columns;
import com.hongkuncheng.cms.model.Datamodel;
import com.hongkuncheng.cms.model.Webinfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 专题频道管理
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/zhuanti")
public class AzhuantiController extends BaseController {

    // 专题频道列表
    @RequestMapping
    public String index() {
        List<Zhuanti> zhuantis = factorySession.createCriteria(Zhuanti.class)
                .addOrder(Order.asc("sort")).list();
        request.setAttribute("zhuantis", zhuantis);
        return view();
    }

    // 专题频道编辑
    @RequestMapping("edit")
    public String edit() {
        int zhuantiId = request.getParameter("zhuantiid") == null ? 0 :
                Integer.parseInt(request.getParameter("zhuantiid"));
        List<Datamodel> datamodels = factorySession.createCriteria(Datamodel.class)
                .add(Restrictions.eq("datamodelType.id", 2)).list();
        if (datamodels.size() == 0) {
            PageHelper.alertAndGoBack("未找到可用的其它类数据模型");
            return null;
        }
        Zhuanti zhuanti = zhuantiId == 0 ? new Zhuanti() : factorySession.get(Zhuanti.class, zhuantiId);
        if (zhuantiId == 0) {
            Object uniqueResult = factorySession.createCriteria(Zhuanti.class)
                    .setProjection(Projections.max("sort")).uniqueResult();
            int maxSort = uniqueResult == null ? 0 : Integer.parseInt(uniqueResult.toString());
            zhuanti.setSort(++maxSort);
        }
        request.setAttribute("zhuanti", zhuanti);
        request.setAttribute("datamodels", datamodels);
        webinfo = factorySession.get(Webinfo.class, 1);
        List<File> templatefiles = FileHelper.getFiles(applicationDirectory + "/template/" +
                webinfo.getTemplate() + "/");
        request.setAttribute("templatefiles", templatefiles);
        return view();
    }

    // 专题频道编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_webmaintain_zhuanti");
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        String name = request.getParameter("name");
        String urldirectory = request.getParameter("urldirectory");
        int exist = Integer.parseInt(factorySession.createCriteria(Zhuanti.class)
                .add(Restrictions.eq("name", name)).add(Restrictions.ne("id", zhuantiId))
                .setProjection(Projections.count("id")).uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("频道名称已存在，请重新输入");
            return;
        }
        if (DirectoryHelper.checkSpecial(urldirectory)) {
            PageHelper.alertAndGoBack("URL目录为系统特殊目录，您不能使用，请重新输入");
        }
        exist = Integer.parseInt(factorySession.createCriteria(Zhuanti.class)
                .add(Restrictions.eq("urldirectory", urldirectory))
                .add(Restrictions.ne("id", zhuantiId))
                .setProjection(Projections.count("id"))
                .uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("URL目录已在频道频道中存在，请重新输入");
            return;
        }
        exist = Integer.parseInt(factorySession.createCriteria(Columns.class)
                .add(Restrictions.eq("urldirectory", urldirectory))
                .add(Restrictions.eq("parentId", 0))
                .setProjection(Projections.count("id"))
                .uniqueResult().toString());
        if (exist > 0) {
            PageHelper.alertAndGoBack("URL目录已在栏目中存在，请重新输入");
            return;
        }
        int datamodelId = Integer.parseInt(request.getParameter("datamodelid"));
        String template = request.getParameter("template");
        Zhuanti zhuanti = zhuantiId == 0 ? new Zhuanti() :
                factorySession.load(Zhuanti.class, zhuantiId, LockMode.PESSIMISTIC_WRITE);
        zhuanti.setDatamodel(factorySession.load(Datamodel.class, datamodelId));
        zhuanti.setTemplate(template);
        zhuanti.setName(name);
        zhuanti.setUrldirectory(urldirectory);
        zhuanti.setImage(request.getParameter("image"));
        zhuanti.setSort(Integer.parseInt(request.getParameter("sort")));
        if (zhuantiId == 0) factorySession.save(zhuanti);
        recordLog((zhuantiId == 0 ? "新建" : "修改") + "专题频道《" + zhuanti.getName().trim() + "》");
        PageHelper.confirmAndRedirect("保存成功，是否生成相关静态页面？",
                "../createpage/zhuantifilepaths?zhuantiids=" + zhuantiId + "&temp=1", "./");
    }

    // 专题频道删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_webmaintain_zhuanti");
        int zhuantiId = Integer.parseInt(request.getParameter("zhuantiid"));
        Zhuanti zhuanti = factorySession.load(Zhuanti.class, zhuantiId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(zhuanti);
        PageHelper.alertAndRedirect("删除成功", "./");
    }

}
